package com.econ.powercloud.ems.observer;

import com.econ.powercloud.datahandle.facade.domain.ProcessedRunData;
import com.econ.powercloud.ems.common.interf.EmsYcYxStrategyObserver;
import com.econ.powercloud.ems.config.EMSStaticMap;
import com.econ.powercloud.ems.dal.entity.EmsAdditionalOperationConfig;
import com.econ.powercloud.ems.observer.strategy.YcYxAbstractStrategy;
import com.econ.powercloud.ems.service.dao.EmsYcYxStrategyConfigDAO;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * ycyx策略入接口
 *
 * @author hcx
 * @since 2023/6/28 14:46
 **/
@Component
public class YcYxStrategyProcessor implements EmsYcYxStrategyObserver {

    @Autowired
    EmsYcYxStrategyConfigDAO emsYcYxStrategyConfigDAO;
    @Autowired
    List<YcYxAbstractStrategy> strategyList;

    /**
     * condition 判断符号
     */
    final String CONDITION_EQUALS = "=";

    /**
     * 策略Map
     */
    private Map<Integer, YcYxAbstractStrategy> strategyMap = Maps.newHashMap();

    /**
     * 不同类型策略所需数据列表 type 1 -> list[0]
     */
    private List<List<String>> requestDataList;

    @PostConstruct
    private void init() {
        strategyMap = strategyList.stream().collect(Collectors.toMap(YcYxAbstractStrategy::getType, item -> item));

    }

    @Override
    public void doInvoke(List<ProcessedRunData> data) {
        //查询已配置策略并进行条件判断
        requestDataList = new ArrayList<>(strategyList.size());
        for (int i = 0; i < strategyList.size(); i++) {
            //初始化列表
            requestDataList.add(new ArrayList<>());
        }
        data.forEach(d -> {
            String key = d.getPid();
            if (EMSStaticMap.emsYcYxStrategyConfigMap.containsKey(key)) {
                //如果缓存存在策略配置,则进行逐个条件判断
                List<EmsAdditionalOperationConfig> emsAdditionalOperationConfigList = EMSStaticMap.emsYcYxStrategyConfigMap.get(key);
                emsAdditionalOperationConfigList.forEach(c -> {
                    if (c.getCondition() != null && c.getValue() != null) {
                        //条件不为空则进行判断
                        switch (c.getCondition()) {
                            case CONDITION_EQUALS:
                                //进行条件判断
                                int nowValue = Integer.parseInt(d.getVal());
                                int conditionValue = Integer.parseInt(c.getValue());
                                if (nowValue == conditionValue) {
                                    //如果成立，则加入对应的策略的数据List，后续传入策略
                                    requestDataList.get(c.getType() - 1).add(c.getOperation());
                                }
                                break;
                        }
                    }
                });
            }
        });

        //对应数据进入相应策略处理
        for (int i = 0; i < requestDataList.size(); i++) {
            //i = 0时,requestDataList[0] 是传入策略 type:1 的数据
            YcYxAbstractStrategy strategy = strategyMap.get(i + 1);
            strategy.strategy(requestDataList.get(i));
        }

    }
}
